<?php
/**
 * file created on 29-mar-2006
 *
 * jaco_at_pixeldump_dot_org
 */

$svgColorNames = array();
$svgColorNames["white"] 			= "#FFFFFF";
$svgColorNames["black"] 			= "#000000";
$svgColorNames["navy"] 				= "#000080";
$svgColorNames["darkblue"] 			= "#00008B";
$svgColorNames["mediumblue"] 		= "#0000CD";
$svgColorNames["blue"] 				= "#0000FF";
$svgColorNames["darkgreen"] 		= "#006400";
$svgColorNames["green"] 			= "#008000";
$svgColorNames["teal"] 				= "#008080";
$svgColorNames["darkcyan"] 			= "#008B8B";
$svgColorNames["deepskyblue"] 		= "#00BFFF";
$svgColorNames["darkturquoise"] 	= "#00CED1";
$svgColorNames["mediumspringgreen"] = "#00FA9A";
$svgColorNames["lime"] 				= "#00FF00";
$svgColorNames["springgreen"] 		= "#00FF7F";
$svgColorNames["aqua"] 				= "#00FFFF";
$svgColorNames["cyan"] 				= "#00FFFF";
$svgColorNames["midnightblue"] 		= "#191970";
$svgColorNames["dodgerblue"] 		= "#1E90FF";
$svgColorNames["lightseagreen"] 	= "#20B2AA";
$svgColorNames["forestgreen"] 		= "#228B22";
$svgColorNames["seagreen"] 			= "#2E8B57";
$svgColorNames["darkslategray"] 	= "#2F4F4F";
$svgColorNames["limegreen"] 		= "#32CD32";
$svgColorNames["mediumseagreen"] 	= "#3CB371";
$svgColorNames["turquoise"]			= "#40E0D0";
$svgColorNames["royalblue"] 		= "#4169E1";
$svgColorNames["steelblue"]			= "#4682B4";
$svgColorNames["darkslateblue"] 	= "#483D8B";
$svgColorNames["mediumturquoise"] 	= "#48D1CC";
$svgColorNames["indigo"] 			= "#4B0082";
$svgColorNames["darkolivegreen"] 	= "#556B2F";
$svgColorNames["cadetblue"] 		= "#5F9EA0";
$svgColorNames["cornflowerblue"] 	= "#6495ED";
$svgColorNames["mediumaquamarine"] 	= "#66CDAA";
$svgColorNames["dimgray"] 			= "#696969";
$svgColorNames["slateblue"] 		= "#6A5ACD";
$svgColorNames["olivedrab"] 		= "#6B8E23";
$svgColorNames["slategray"] 		= "#708090";
$svgColorNames["lightslategray"] 	= "#778899";
$svgColorNames["mediumslateblue"] 	= "#7B68EE";
$svgColorNames["lawngreen"] 		= "#7CFC00";
$svgColorNames["chartreuse"] 		= "#7FFF00";
$svgColorNames["aquamarine"] 		= "#7FFFD4";
$svgColorNames["maroon"] 			= "#800000";
$svgColorNames["purple"] 			= "#800080";
$svgColorNames["olive"] 			= "#808000";
$svgColorNames["gray"] 				= "#808080";
$svgColorNames["skyblue"] 			= "#87CEEB";
$svgColorNames["lightskyblue"] 		= "#87CEFA";
$svgColorNames["blueviolet"] 		= "#8A2BE2";
$svgColorNames["darkred"] 			= "#8B0000";
$svgColorNames["darkmagenta"] 		= "#8B008B";
$svgColorNames["saddlebrown"] 		= "#8B4513";
$svgColorNames["darkseagreen"] 		= "#8FBC8F";
$svgColorNames["lightgreen"] 		= "#90EE90";
$svgColorNames["mediumpurple"] 		= "#9370DB";
$svgColorNames["darkviolet"] 		= "#9400D3";
$svgColorNames["palegreen"] 		= "#98FB98";
$svgColorNames["darkorchid"] 		= "#9932CC";
$svgColorNames["yellowgreen"] 		= "#9ACD32";
$svgColorNames["sienna"] 			= "#A0522D";
$svgColorNames["brown"] 			= "#A52A2A";
$svgColorNames["darkgray"] 			= "#A9A9A9";
$svgColorNames["lightblue"] 		= "#ADD8E6";
$svgColorNames["greenyellow"] 		= "#ADFF2F";
$svgColorNames["paleturquoise"] 	= "#AFEEEE";
$svgColorNames["lightsteelblue"] 	= "#B0C4DE";
$svgColorNames["powderblue"] 		= "#B0E0E6";
$svgColorNames["firebrick"] 		= "#B22222";
$svgColorNames["darkgoldenrod"] 	= "#B8860B";
$svgColorNames["mediumorchid"] 		= "#BA55D3";
$svgColorNames["rosybrown"] 		= "#BC8F8F";
$svgColorNames["darkkhaki"] 		= "#BDB76B";
$svgColorNames["silver"] 			= "#C0C0C0";
$svgColorNames["mediumvioletred"] 	= "#C71585";
$svgColorNames["indianred"] 		= "#CD5C5C";
$svgColorNames["peru"] 				= "#CD853F";
$svgColorNames["chocolate"] 		= "#D2691E";
$svgColorNames["tan"] 				= "#D2B48C";
$svgColorNames["lightgrey"] 		= "#D3D3D3";
$svgColorNames["thistle"] 			= "#D8BFD8";
$svgColorNames["orchid"] 			= "#DA70D6";
$svgColorNames["goldenrod"] 		= "#DAA520";
$svgColorNames["palevioletred"] 	= "#DB7093";
$svgColorNames["crimson"] 			= "#DC143C";
$svgColorNames["gainsboro"] 		= "#DCDCDC";
$svgColorNames["plum"] 				= "#DDA0DD";
$svgColorNames["burlywood"] 		= "#DEB887";
$svgColorNames["lightcyan"] 		= "#E0FFFF";
$svgColorNames["lavender"] 			= "#E6E6FA";
$svgColorNames["darksalmon"] 		= "#E9967A";
$svgColorNames["violet"] 			= "#EE82EE";
$svgColorNames["palegoldenrod"] 	= "#EEE8AA";
$svgColorNames["lightcoral"] 		= "#F08080";
$svgColorNames["khaki"] 			= "#F0E68C";
$svgColorNames["aliceblue"] 		= "#F0F8FF";
$svgColorNames["honeydew"] 			= "#F0FFF0";
$svgColorNames["azure"] 			= "#F0FFFF";
$svgColorNames["sandybrown"] 		= "#F4A460";
$svgColorNames["wheat"] 			= "#F5DEB3";
$svgColorNames["beige"] 			= "#F5F5DC";
$svgColorNames["whitesmoke"] 		= "#F5F5F5";
$svgColorNames["mintcream"] 		= "#F5FFFA";
$svgColorNames["ghostwhite"] 		= "#F8F8FF";
$svgColorNames["salmon"] 			= "#FA8072";
$svgColorNames["antiquewhite"] 		= "#FAEBD7";
$svgColorNames["linen"] 			= "#FAF0E6";
$svgColorNames["lightgoldenrodyellow"] = "#FAFAD2";
$svgColorNames["oldlace"] 			= "#FDF5E6";
$svgColorNames["red"] 				= "#FF0000";
$svgColorNames["fuchsia"] 			= "#FF00FF";
$svgColorNames["magenta"] 			= "#FF00FF";
$svgColorNames["deeppink"] 			= "#FF1493";
$svgColorNames["orangered"] 		= "#FF4500";
$svgColorNames["tomato"] 			= "#FF6347";
$svgColorNames["hotpink"] 			= "#FF69B4";
$svgColorNames["coral"] 			= "#FF7F50";
$svgColorNames["darkorange"] 		= "#FF8C00";
$svgColorNames["lightsalmon"] 		= "#FFA07A";
$svgColorNames["orange"] 			= "#FFA500";
$svgColorNames["lightpink"] 		= "#FFB6C1";
$svgColorNames["pink"] 				= "#FFC0CB";
$svgColorNames["gold"] 				= "#FFD700";
$svgColorNames["peachpuff"] 		= "#FFDAB9";
$svgColorNames["navajowhite"] 		= "#FFDEAD";
$svgColorNames["moccasin"] 			= "#FFE4B5";
$svgColorNames["bisque"] 			= "#FFE4C4";
$svgColorNames["mistyrose"] 		= "#FFE4E1";
$svgColorNames["blanchedalmond"] 	= "#FFEBCD";
$svgColorNames["papayawhip"] 		= "#FFEFD5";
$svgColorNames["lavenderblush"] 	= "#FFF0F5";
$svgColorNames["seashell"] 			= "#FFF5EE";
$svgColorNames["cornsilk"] 			= "#FFF8DC";
$svgColorNames["lemonchiffon"] 		= "#FFFACD";
$svgColorNames["floralwhite"] 		= "#FFFAF0";
$svgColorNames["snow"] 				= "#FFFAFA";
$svgColorNames["yellow"] 			= "#FFFF00";
$svgColorNames["lightyellow"] 		= "#FFFFE0";
$svgColorNames["ivory"] 			= "#FFFFF0";

// look-up svg color names table
$svgCNLT = array_keys($svgColorNames);

function is_svgColorName($cnTest){
	global $svgCNLT;
	return in_array($cnTest, $svgCNLT, true);
}

function get_svgColorValue($colorName){
	global $svgColorNames, $svgCNLT;

	$colorName = strtolower($colorName);
	if(in_array($colorName, $svgCNLT, true)){
		return $svgColorNames[$colorName];
	}
	else return $colorName;
}

/**
 * reads string
 * fill an associative array with drawing coords
 */
function get_drawing_coords($cStr){

	$cmds = "cClLmMz";
	$ca = array();

	$cStr = preg_replace("/,/", " ", trim($cStr));
	$cStr = preg_replace("/(\D)(\s)/", "##\${1}##", $cStr);
	$dl = explode("##", trim($cStr));
	$dCount = count($dl);
	$cmdAr = array();
	$cmdStr = "";
	$drawingCommands = array();
	$coordGroup = array();

	$lastCoordX = 0;
	$lastCoordY = 0;

	for($i = 1; $i < $dCount; $i += 2) {

		$cmd = $dl[$i];

		$cidx = count($ca);

		$coords = explode(" ", trim($dl[$i + 1]));

		for($j = 0; $j < count($coords); $j++) $coords[$j] = (float) $coords[$j];

		if($cmd == "M"){

			if(strlen($cmdStr)){
				$cmdAr[] = $cmdStr;
				$coordGroup[] = $ca;
			}

			$ca = array();
			$cmdStr = "M";
			$cidx = 0;
			$ca[0]["x"] = $coords[0];
			$ca[0]["y"] = $coords[1];

			$lastCoordX = $coords[0];
			$lastCoordY = $coords[1];

			$caCount = count($coords);

			if($caCount > 2){

				//$lts = floor($caCount / 2);

				for($j = 2; $j < $caCount; $j += 2){

					$cidx = count($ca);
					$ca[$cidx]["x"] = $coords[$j];
					$ca[$cidx]["y"] = $coords[$j + 1];
					$cmdStr .= "L";
					$lastCoordX = $ca[$cidx]["x"];
					$lastCoordY = $ca[$cidx]["y"];
				}
			}
		}
		else if($cmd == "m"){

			if(strlen($cmdStr)){
				$cmdAr[] = $cmdStr;
				$coordGroup[] = $ca;
			}

			//print_r($coords);

			$ca = array();
			$cmdStr = "M";
			$cidx = 0;
			$ca[0]["x"] = $lastCoordX + $coords[0];
			$ca[0]["y"] = $lastCoordY + $coords[1];

			$lastCoordX = $coords[0];
			$lastCoordY = $coords[1];

			$caCount = count($coords);

			if($caCount > 2){

				//$lts = floor($caCount / 2);

				for($j = 2; $j < $caCount; $j += 2){

					$cidx = count($ca);
					$ca[$cidx]["x"] = $lastCoordX + $coords[$j];
					$ca[$cidx]["y"] = $lastCoordY + $coords[$j + 1];
					$cmdStr .= "L";
					$lastCoordX = $ca[$cidx]["x"];
					$lastCoordY = $ca[$cidx]["y"];
				}
			}
		}
		else if($cmd == "L"){

			$ca[$cidx]["x"] = (float) $coords[0];
			$ca[$cidx]["y"] = (float) $coords[1];
			$cmdStr .= "L";
			$lastCoordX = $ca[$cidx]["x"];
			$lastCoordY = $ca[$cidx]["y"];
		}
		else if($cmd == "l"){

			$ca[$cidx]["x"] = $lastCoordX + $coords[0];
			$ca[$cidx]["y"] = $lastCoordY + $coords[1];
			$cmdStr .= "L";
			$lastCoordX = $ca[$cidx]["x"];
			$lastCoordY = $ca[$cidx]["y"];
		}
		else if($cmd == "C"){

			$ca[$cidx]["c1x"] = $coords[0];
			$ca[$cidx]["c1y"] = $coords[1];
			$ca[$cidx]["c2x"] = $coords[2];
			$ca[$cidx]["c2y"] = $coords[3];
			$ca[$cidx]["ax"]  = $coords[4];
			$ca[$cidx]["ay"]  = $coords[5];
			$cmdStr .= "C";
			$lastCoordX = $ca[$cidx]["ax"];
			$lastCoordY = $ca[$cidx]["ay"];
		}
		else if($cmd == "c"){

			$ca[$cidx]["c1x"] = $lastCoordX + $coords[0];
			$ca[$cidx]["c1y"] = $lastCoordY + $coords[1];
			$ca[$cidx]["c2x"] = $lastCoordX + $coords[2];
			$ca[$cidx]["c2y"] = $lastCoordY + $coords[3];
			$ca[$cidx]["ax"]  = $lastCoordX + $coords[4];
			$ca[$cidx]["ay"]  = $lastCoordY + $coords[5];
			$cmdStr .= "C";
			$lastCoordX = $ca[$cidx]["ax"];
			$lastCoordY = $ca[$cidx]["ay"];
		}
	}

	$cmdAr[] = $cmdStr;
	$coordGroup[] = $ca;


	$drawingCommands["commands"] = $cmdAr;
	$drawingCommands["coordGroup"] = $coordGroup;

	return $drawingCommands;
}


/**
 * very similar to get_boundary, located in mk_lib_draw
 * just a different name for xperimental use
 */
function get_extents($coords){
	$xcoords = array();
	$ycoords = array();

	foreach($coords as $c){
		if(isset($c["x"])) $xcoords[] = $c["x"];
		else if(isset($c["y"])) $ycoords[] = $c["y"];
		else if(isset($c["ax"])) $xcoords[] = $c["ax"];
		else if(isset($c["ay"])) $ycoords[] = $c["ay"];
	}

	$extents = array();
	$extents["xmin"] = min($xcoords);
	$extents["xmax"] = max($xcoords);
	$extents["ymin"] = min($ycoords);
	$extents["ymax"] = max($ycoords);

	return $extents;
}

/**
 *
 */
function get_styleInfo($sStr, $transform = array()){

	$styleInfo = array();
	if(!strlen($sStr)) return;

	$fsa = array();
	$fsb = array();

	$fsa = explode(";", trim($sStr));

	foreach($fsa as $fs){
		$kv = explode(":", $fs);

		$fsb[] =  array();
		if(count($kv) > 1) $fsb[$kv[0]] = $kv[1];
	}


	if(isset($fsb["fill-rule"]) && !isset($fsb["fill"]))
		$styleInfo["fillColor"] = get_color_array("#000000", 0xff, true);

	if(isset($fsb["fill"])){

		if($fsb["fill"] == "none") unset($styleInfo["fillColor"]);
		else {
			$cAr = parse_color($fsb["fill"]);
			if(is_array($cAr))	$styleInfo["fillColor"] = $cAr; // color values
			else $styleInfo["fillColor"]["refID"] = $cAr; 		// Xpointer urlID

			if(isset($fsb["fill-opacity"])){
				$alpha = (float)$fsb["fill-opacity"];
				$styleInfo["fillColor"]["alpha"] = round($alpha * 0xff);
			}
			if(isset($fsb["opacity"])){
				$alpha = (float)$fsb["opacity"];
				$styleInfo["fillColor"]["alpha"] = round($alpha * 0xff);
			}
		}
	}

	if(isset($fsb["stroke"])){

		if($fsb["stroke"] == "none") unset($styleInfo["strokeColor"]);
		else {
			$styleInfo["strokeColor"] = parse_color($fsb["stroke"]);

			if(isset($fsb["stroke-opacity"])){
				$alpha = (float) $fsb["stroke-opacity"];
				$styleInfo["strokeColor"]["alpha"] = round($alpha * 0xff);
			}

			$strokeWidth = (float) isset($fsb["stroke-width"]) ? $fsb["stroke-width"] : 1;

			/* FIXME: not a reliable stroke scaling method */
			if(isset($transform["type"])){
				$type = $transform["type"];
				if($type == "matrix"){
					$p = apply_matrix(0, 0, $transform["factors"]);
					$strokeWidth *= (abs($p["x"]) / 100);
				}
			}

			$styleInfo["strokeWidth"] = $strokeWidth;
		}
	}

	return $styleInfo;
}



/**
 * apply matrix tranformation to a coord x,y
 * optional nameX, nameY are useful when applying transform
 * to other kind of vertices, e.g. bezier control points
 */
function apply_matrix($x = 0, $y = 0, $matrix = array(), $nameX = "x", $nameY = "y"){
	if(!count($matrix)) return false;

	$point = array();
	$point[$nameX] = $x * $matrix["a"] + $y * $matrix["c"] + $matrix["e"];
	$point[$nameY] = $x * $matrix["b"] + $y * $matrix["d"] + $matrix["f"];

	return $point;
}
?>
